package com.danan.realtime.app;

import com.danan.realtime.util.ResourceBundleUtil;
import com.danan.realtime.util.SourceUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author NanHuang
 * @Date 2023/1/24
 */
public abstract class BaseApp {
    public void init(Integer port,Integer parallelism,String groupId,String topic){
        System.setProperty("HADOOP_USER_NAME","danan");
        // 1 创建编程环境
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",port);//设置前web端口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(parallelism);//设置并行度
        // 2 设置状态后端和Checkpoint
        env.enableCheckpointing(3000);//设置checkpoint的周期
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);//设置同时checkpoint的数量
        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000);//checkpoint的超时时间
        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setCheckpointStorage(ResourceBundleUtil.getProperty("checkpoint.storage.url.prefix") + groupId);//设置checkpoint的存储路径
        // 3 从Kafka消费数据
        KafkaSource<String> kafkaSource = SourceUtil.getKafkaSource(topic, groupId);
        DataStreamSource<String> ds = env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafka");
        // 4 对数据进行处理
        invoke(env,ds);
        // 5 运行编程环境
        try {
            env.execute(groupId);//给应用设置名称
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public abstract void invoke(StreamExecutionEnvironment env, DataStreamSource<String> ds);
}
